/**
 * copyright(c) 2013 www.jiongshibaoliao.com, All Rights Reserved.
 * project: jiongshibaoliao 
 * author: 曾帅
 * date: 2013-12-29 下午04:32:52
 * version: v1.0
 */

package com.jiongshibaoliao.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jiongshibaoliao.common.constants.Constants;
import com.jiongshibaoliao.common.mybatis.Page;
import com.jiongshibaoliao.common.mybatis.PageBean;
import com.jiongshibaoliao.service.ArticleService;
import com.jiongshibaoliao.util.VerifyCode;

/**
 * @project jiongshibaoliao
 * @author 曾帅
 * @version 1.0
 * @date 2013-12-29 下午04:32:52
 */

@Controller
public class MainController extends BaseController {
	
	private static final Logger logger = Logger.getLogger(MainController.class);
	
	@Autowired
	public ArticleService articleService;

	@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST })
	public String main(PageBean pageBean, HttpServletRequest request, ModelMap model) {

		return this.hot(pageBean, request, model);
	}
	
	@RequestMapping(value = "/hot", method = { RequestMethod.GET, RequestMethod.POST })
	public String hot(PageBean pageBean, HttpServletRequest request, ModelMap model) {
		Page page = articleService.getHotArticlePageList(pageBean);
		model.put("articleList", page.getlistResult());
		model.put("pageBean", page.getPageBean());

		return "main";
	}
	
	@RequestMapping(value = "/most", method = { RequestMethod.GET, RequestMethod.POST })
	public String most(PageBean pageBean, HttpServletRequest request, ModelMap model) {
		Page page = articleService.getMostArticlePageList(pageBean);
		model.put("articleList", page.getlistResult());
		model.put("pageBean", page.getPageBean());

		return "main";
	}
	
	@RequestMapping(value = "/real", method = { RequestMethod.GET, RequestMethod.POST })
	public String real(PageBean pageBean, HttpServletRequest request, ModelMap model) {
		Page page = articleService.getRealArticlePageList(pageBean);
		model.put("articleList", page.getlistResult());
		model.put("pageBean", page.getPageBean());

		return "main";
	}
	
	@RequestMapping(value = "/jiong", method = { RequestMethod.GET, RequestMethod.POST })
	public String jiong(HttpServletRequest request, ModelMap model) {
		Map<String, Object> articleMap = articleService.getAuditArticleRandom();
		model.put("articleMap", articleMap);

		return "jiong";
	}
	
	@RequestMapping(value = "/verifyCode", method = { RequestMethod.GET, RequestMethod.POST })
	public void verifyCode(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		response.setContentType("image/png");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);

		try {
			new VerifyCode().getCode(request, response);
		} catch (Exception e) {
			logger.error("verifyCode get exception：", e);
		}
	}
	
	@RequestMapping(value = "/setNavClass", method = {RequestMethod.GET, RequestMethod.POST})
	public void setNavClass(@RequestBody String jsonStr, HttpServletRequest request, ModelMap model) {
		JSONObject json = JSONObject.fromObject(jsonStr);
		String navId = String.valueOf(json.get("navId"));
		
		request.getSession().setAttribute(Constants.NAV_SESSION_KEY, navId);
	}
}
